var colors = ["blue","yellow","red","green","orangered","black"]
var sizes = ["mini","little","small","medium","big","bigger","biggest"]
var speeds = ["low", "normal", "fast"]
let timers =[]
let send =  document.getElementById("send")

let sum =0;
window.chat = {
    socket: null,
    init: function () {
        if (window.WebSocket) {
            chat.socket = new WebSocket("ws://127.0.0.1:8091/ws");
            chat.socket.onopen = function () {
                console.log("连接建立成功");
            },
                chat.socket.onclose = function () {
                    console.log("连接关闭");
                },
                chat.socket.onerror = function () {
                    console.log("连接出错");
                },
                chat.socket.onmessage = function (e) {
                    console.log("接受到消息" + e.data);
                    let strs = e.data.split("---");
                    let elem = getElem(strs[0]);
                    let elems = elem.split("$");
                    $("#receiveMsg").append(elems[0])
                    let timer = setTimeout(function () {
                        let oo = $(".start").addClass("end");
                        timers.push(timer)
                    },500)
                    // let timer2 = setTimeout(function () {
                    //     let a = "#"+elems[1]
                    //     $(a).remove();
                    //     timers.push(timer2)
                    // },13000)
                    let liem = getLsElem(strs)
                    $("#ls-content").append(liem)
                    $("#ls-content").scrollTop(1000);
                    let timer3 = setTimeout(function () {
                        $(".ls-em").removeClass("ls-em-noactive")
                        $(".ls-em").addClass("ls-em-active");
                        clearTimeout(timer3)
                    },100)
                }
        } else {
            alert("浏览器不支持协议");
        }

    },
    chat1: function () {
        let msg = editor.txt.html()
        console.log
        if(msg == `<p><br></p>`){
            return;
        }
        chat.socket.send(msg)
        editor.txt.html("")
        send.disabled = true;
        let n =3;
        let timer = setInterval(() => {
            if(n!=0){
                send.innerText = " 稍等 "+--n+"秒"
            }
            else{
                send.disabled = false;
                send.innerText = "发送弹幕";
                clearInterval(timer)
            }
        }, 1000);
    }
};
chat.init();

/**
 * 选择样式
 * @returns {string}
 */
function selectClass(){
    let speed = speeds[Math.floor(Math.random()*3)]
    let color = colors[Math.floor(Math.random()*6)]
    let size = sizes[Math.floor(Math.random()*7)]
    return speed+" "+color+" "+size
}


/**
 * 返回一个elem
 * @param content
 */
function getElem(content) {
    let higt =Math.floor(Math.random()*400)
    console.log(higt)
    let elemclass = 'class = "start absolute '+selectClass()+'" '
    let high = 'style = "margin-top:'+higt+'px;display:flex;justify-content: center"'
    let s = sum++;
    let elem =`<div id="`+s+`" `+ elemclass+high+`>`+ content +`</div> $`+s
    return elem
}
/**
 * 返回一个弹幕列表的elem 
 * @param {*} content 
 */
function getLsElem(content){
    let time = `<span style="font-size:13px">`+content[1]+`</span>`
    let elem = `<div class="ls-em ls-em-noactive">`+time+content[0]+`</div>`
    return elem
}

/**
 * 每秒清除一次计时器
 */
setInterval(function () {
    for (let i=0 ;i<timers.length;i++){
        let timer = timers.pop()
        clearTimeout(timer)
    }
}, 1000)